﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using NHibernate;
using NHibernate.Criterion;
using Rodunu.Movies.Data;
using Rodunu.Movies.Interfaces.Movie;

namespace Rodunu.Movies.Logic.Movie
{
    public class MovieLogic : Repository<Contracts.Movie>, IMovie
    {
        #region IMovie Members

        public IList<Contracts.Movie> GetMoviesByLetters(string letters)
        {
            ISession session = DBHelper.GetCurrentSession();
            IList<Contracts.Movie> movies = session.CreateCriteria(typeof(Contracts.Movie))
                .Add(Expression.Like("Name", "%" + letters + "%"))
                .SetMaxResults(30)
                .List<Contracts.Movie>();
            return movies;
        }

        public Contracts.Movie GetMoviesByTitle(string movie)
        {
            ISession session = DBHelper.GetCurrentSession();
            return (Contracts.Movie)session.CreateCriteria(typeof(Contracts.Movie))
                .Add(Expression.Like("Name", movie))
                .UniqueResult();
        }

        public IList<Contracts.Movie> SearchForMovies(string movie)
        {
            if (string.IsNullOrEmpty(movie))
                return new List<Contracts.Movie>();

            ISession session = DBHelper.GetCurrentSession();
            return session.CreateSQLQuery(BuildMovieSearchWhereClause(movie), "Movie", typeof(Contracts.Movie)).List<Contracts.Movie>();
        }

        #endregion

        #region Private Methods

        private string BuildMovieSearchWhereClause(string movie)
        {
            movie = movie.Replace("'", ".");
            string[] keywords = movie.Split('\u0020');

            StringBuilder likeClause = new StringBuilder();
            likeClause.Append("SELECT {Movie.*} FROM Movie {Movie} WHERE");
            for (int i = 0; i < keywords.Length; i++)
            {
                var stringSearch = keywords[i].Trim();
                if (MovieSearchHelper.CheckIfKeyword(stringSearch))
                {
                    if (MovieSearchHelper.IsNumber(stringSearch))
                        stringSearch += "*";
                    SetContains(ref likeClause, stringSearch);
                    if (i < keywords.Length - 1)
                        likeClause.Append(" AND");
                }
            }
            SetContains(ref likeClause, movie);
            return likeClause.ToString();
        }

        private void SetContains(ref StringBuilder stringToAddName, string movieName)
        {
            stringToAddName.Append(@" CONTAINS([Name],'""");
            stringToAddName.Append(movieName);
            stringToAddName.Append(@"""')");
        }

        #endregion
    }
}


//using System;
//using System.Collections;
//using System.Collections.Generic;
//using System.Text;
//using System.Text.RegularExpressions;
//using NHibernate;
//using NHibernate.Criterion;
//using Rodunu.Movies.Data;
//using Rodunu.Movies.Interfaces.Movie;

//namespace Rodunu.Movies.Logic.Movie
//{
//    public class MovieLogic : Repository<Contracts.Movie>, IMovie
//    {
//        #region IMovie Members

//        public IList<Contracts.Movie> GetMoviesByLetters(string letters)
//        {
//            ISession session = DBHelper.GetCurrentSession();
//            IList<Contracts.Movie> movies = session.CreateCriteria(typeof (Contracts.Movie))
//                .Add(Expression.Like("Name", "%" + letters + "%"))
//                .SetMaxResults(30)
//                .List<Contracts.Movie>();
//            //DBHelper.CloseSession();
//            return movies;
//        }

//        public Contracts.Movie GetMoviesByTitle(string movie)
//        {
//            ISessionFactory sessionFactory = DBHelper.BuildConfiguration().Configure().BuildSessionFactory();
//            ISession session = sessionFactory.OpenSession();

//            var newMovie = (Contracts.Movie)session.CreateCriteria(typeof (Contracts.Movie))
//                .Add(Expression.Like("Name", movie))
//                .UniqueResult();

//            session.Close();
//            return newMovie;
//        }

//        public IList<Contracts.Movie> SearchForMovies(string movie)
//        {
//            if (string.IsNullOrEmpty(movie))
//                return new List<Contracts.Movie>();
//            movie = movie.Replace("'", ".");
//            string[] keywords = movie.Split('\u0020');
//            string likeClause = string.Empty;

//            foreach (string s in keywords)
//            {
//                if (!string.IsNullOrEmpty(s) && MovieSearchHelper.CheckIfKeyword(s))
//                {
//                    var stringSearch = s.Trim();
//                    stringSearch = stringSearch.Replace("'", ".");
//                    if (MovieSearchHelper.IsNumber(stringSearch))
//                        stringSearch += "*";
//                    likeClause += @" CONTAINS([Name],'""" + stringSearch + @"""') AND";
//                }
//            }

//            if (string.IsNullOrEmpty(likeClause))
//                return new List<Contracts.Movie>();

//            likeClause = likeClause.Substring(0, likeClause.Length - 4);
//            likeClause += @" OR CONTAINS([Name],'""" + movie + @"""')";
//            string sqlCommand = "SELECT {Movie.*} " +
//                                "FROM Movie {Movie} WHERE" + likeClause;
//            ISession session = DBHelper.GetCurrentSession();
//            return session.CreateSQLQuery(sqlCommand, "Movie", typeof(Contracts.Movie)).List<Contracts.Movie>();
//        }

//        #endregion

//    }
//}
